package se.kth.fericss.tree;

import static org.junit.Assert.*;

import org.junit.Test;

public class TreeTest {

	@Test
	public void testTree() {
		//Check if you can create different tree's
		Tree<Integer> tree = new Tree<Integer>();
		Tree<String> stringTree = new Tree<String>();
	}

	@Test
	public void testToString() {
		Tree<Integer> tree = new Tree<Integer>();
		assertTrue(tree.add(5));
		assertTrue(tree.add(1));
		assertTrue(tree.add(666));
		assertTrue(tree.add(14));
		assertTrue(tree.add(3));
		assertTrue(tree.add(44));
		assertTrue(tree.add(2));
		assertTrue(tree.add(0));
		assertTrue(tree.add(21));
		assertTrue(!tree.add(21));
		//System.out.println(tree);
		//Create a tree and check if the string representation is correct
		assertTrue("[0, 1, 2, 3, 5, 14, 21, 44, 666]".equals(""+tree));
	}

	@Test
	public void testGetLeaves() {
		Tree<Integer> tree = new Tree<Integer>();
		//create a Tree and check if the leaves matches the intended amount
		assertTrue(tree.add(5));
		assertTrue(tree.getLeaves()==1);
		assertTrue(tree.add(1));
		assertTrue(tree.getLeaves()==1);
		assertTrue(tree.add(666));
		assertTrue(tree.getLeaves()==2);
		assertTrue(tree.add(14));
		assertTrue(tree.add(3));
		assertTrue(tree.add(44));
		assertTrue(tree.add(2));
		assertTrue(tree.add(0));
		assertTrue(tree.add(21));
		assertTrue(!tree.add(21));
		assertTrue(tree.getLeaves()==3);
		assertTrue(tree.add(667));
		assertTrue(tree.getLeaves()==4);
	}

	@Test
	public void testContains() {
		Tree<Integer> tree = new Tree<Integer>();
		//create a tree and check if it contains all the inserted values
		assertTrue(tree.add(5));
		assertTrue(tree.add(14));
		assertTrue(tree.add(3));
		assertTrue(tree.add(44));
		assertTrue(tree.add(2));
		assertTrue(tree.add(21));
		assertTrue(!tree.add(21));
		
		assertTrue(tree.contains(21));
		assertTrue(tree.contains(5));
		assertTrue(tree.contains(14));
		assertTrue(tree.contains(44));
		assertTrue(tree.contains(3));
		assertTrue(!tree.contains(4441));
		assertTrue(!tree.contains(null));
	}

	@Test
	public void testGetDepth() {
		Tree<Integer> tree = new Tree<Integer>();
		//create a tree and check that it's depth is correct
		assertTrue(tree.getDepth()==0);
		assertTrue(tree.add(5));
		assertTrue(tree.getDepth()==1);
		assertTrue(tree.add(4));
		assertTrue(tree.add(3));
		assertTrue(tree.add(2));
		assertTrue(tree.getDepth()==4);
		assertTrue(tree.add(1));
		assertTrue(tree.add(21));
		assertTrue(tree.add(51));
		assertTrue(tree.getDepth()==5);
		tree = new Tree<Integer>();
		assertTrue(tree.getDepth()==0);
		assertTrue(tree.add(6));
		assertTrue(tree.add(8));
		assertTrue(tree.add(9));
		assertTrue(tree.add(7));
		assertTrue(tree.add(5));
		assertTrue(tree.add(4));
		assertTrue(tree.add(3));
		assertTrue(tree.add(1));
		assertTrue(tree.add(2));
		assertTrue(tree.getDepth()==6);
	}

	@Test
	public void testAdd() {
		//create a tree and insert different values
		Tree<Integer> tree = new Tree<Integer>();
		assertTrue(tree.add(5));
		assertTrue(tree.add(14));
		assertTrue(tree.add(3));
		assertTrue(tree.add(44));
		assertTrue(tree.add(2));
		assertTrue(tree.add(21));
		assertTrue(!tree.add(21));
		assertTrue(!tree.add(null));
	}

	@Test
	public void testGetSize() {
		Tree<Integer> tree = new Tree<Integer>();
		assertTrue(tree.getSize()==0);
		assertTrue(tree.add(5));
		assertTrue(tree.getSize()==1);
		assertTrue(tree.add(14));
		assertTrue(tree.add(3));
		assertTrue(tree.add(44));
		assertTrue(tree.add(2));
		assertTrue(tree.add(21));
		assertTrue(tree.getSize()==6);
		assertTrue(!tree.add(21));
	}

}
